<?php
// +----------------------------------------------------------------------
// | HkCms
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\attachment\controller;

use common\controller\AdminBase;

class Atadmin extends AdminBase {

    //附件存在物理地址
    public $path = '';

    function _initialize() {

        parent::_initialize();

        //附件目录强制/d/file/ 后台设置的附件目录，只对网络地址有效
        $this->path = config("UPLOADFILEPATH");
    }

    /**
     * 附件管理 
     */
    public function index() 
    {
        if ( request()->isPost() ) {

            $this->redirect('index', $_POST);
        }

        $db = db("Attachment");

        $where = array();

        //文件名搜索
        $filename = input('filename', '', 'trim');

        if ( $filename ) {

            $where['filename'] = array('like', '%' . $filename . '%');
        }

        //时间范围搜索
        $start_uploadtime = input('start_uploadtime','');

        $end_uploadtime = input('end_uploadtime','');

        if (!empty($start_uploadtime)) {

            $where['uploadtime'] = array('EGT', strtotime($start_uploadtime));

            if ($end_uploadtime) {

                $where['uploadtime'] = array(array('EGT', strtotime($start_uploadtime)), array('ELT', strtotime($end_uploadtime)), 'AND');
            }
        }

        // 类型
        $fileext = input('fileext','');

        if ($fileext) {

            $where['fileext'] = array('eq', $fileext);
        }
        //附件使用状态
        $status = input('status','');

        if ($status != '') {

            $where['status'] = array('eq', $status);
        }

        $search = [

            'filename' => $filename,

            'start_uploadtime' => $start_uploadtime,

            'end_uploadtime' => $end_uploadtime,

            'fileext' => $fileext,

            'status' => $status
        ];

        $query['m'] = request()->module();
        $query['c'] = request()->controller();
        $query['a'] = request()->action();
        $query['isadmin'] = 1;

        $data = $db->where($where)->order(array("uploadtime" => "DESC"))->paginate(20,false,['query'=>$query]);

        $array = $data->toarray();

        $list = $array['data'];

        foreach ($list as $k => $v) {

            $list[$k]['filesize'] = round($list[$k]['filesize'] / 1024, 2);

            $list[$k]['thumb'] = glob(dirname($this->path . $list[$k]['filepath']) . '/thumb_*' . basename($list[$k]['filepath']));
        }

        $this->assign("data", $data);

        $this->assign("list", $list);

        $this->assign("search", $search);

        return $this->fetch();
    }

    /**
     * 删除附件 get单个删除 post批量删除 
     */
    public function delete() 
    {

        $Attachment = service("Attachment");

        if ( request()->isPost() ) {

            $aid = isset($_POST['aid'])?$_POST['aid']:'';

            if (!$aid) {

                $this->error("请选择！");
            }

            foreach ($aid as $k => $v) {

                if ($Attachment->delFile((int) $v)) {

                    //删除附件关系
                    db("Attachment")->where(array("aid" => $v))->delete();
                }
            }

            $status = true;

        } else {

            $aid = input('aid', 0, 'intval');

            if (empty($aid)) {

                $this->error("缺少参数！");
            }

            if ($Attachment->delFile((int) $aid)) {

                db("Attachment")->where(array("aid" => $aid))->delete();

                $status = true;

            } else {

                $status = false;
            }
        }

        if ($status) {

            $this->success("删除附件成功！");
        } else {
            
            $this->error("删除附件失败！");
        }
    }

}
